package edu.lingnan.librarymanage.controller;

import edu.lingnan.librarymanage.core.vo.BookBaseInfo;
import edu.lingnan.librarymanage.core.vo.PageDataVO;
import edu.lingnan.librarymanage.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author cql
 */
@Controller
public class ViewController {

    @Autowired
    private BookService bookService;


    @GetMapping("/")
    public String indexView() {
        return "redirect:/index?category=0";
    }

    @GetMapping("/index")
    public ModelAndView index2View(@RequestParam(defaultValue = "0", name = "category") Integer categoryId,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "") String name) {
        PageDataVO<BookBaseInfo> result = bookService.findBookByCategory(categoryId, pageSize, current, name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index");
        modelAndView.addObject("page", result);
        return modelAndView;
    }


}
